﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace OnTheWay.Models
{
    [DataBaseTable]
    public class Model
    {
        /// <summary>
        /// 对象唯一标识
        /// </summary>
        [DataMember]
        [DataBaseMember(typeof(Guid), Detail = "标识", IsPrimaryKey = true)]
        public virtual Guid ID { get; set; }
        /// <summary>
        /// 对象名称
        /// </summary>
        [DataMember]
        [DataBaseMember(typeof(string), Detail = "名称", NotNullable = true, Length = 100)]
        public virtual string Name { get; set; }
        /// <summary>
        /// 对象相关的备注
        /// </summary>
        [DataMember]
        [DataBaseMember(typeof(string), Detail = "备注", Length = 100)]
        public virtual string Remark { get; set; }
        /// <summary>
        /// 对象写入数据库的时间
        /// </summary>
        [DataMember]
        [DataBaseMember(typeof(DateTime), Detail = "写入时间", NotNullable = true)]
        public virtual DateTime RegDate { get; set; }


        /// <summary>
        /// 无参构造函数
        /// </summary>
        public Model()
        {
            ID = Guid.NewGuid();
            Name = String.Empty;
            Remark = String.Empty;
            RegDate = DateTime.Now;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="model">OTWObject对象</param>
        public Model(Model model)
        {
            ID = model.ID;
            Name = model.Name;
            Remark = model.Remark;
            RegDate = model.RegDate;
        }

        public virtual T Clone<T>()
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(this));
            }
            catch (System.Exception )
            {
                return default(T);
            }
        }
        /// <summary>
        /// 返回表示当前 ModelBase 的 string。
        /// </summary>
        /// <returns>string，表示当前的 ModelBase。</returns>
        public override string ToString()
        {
            return string.Format("{0} ID：{1}", Name, ID);
        }
    }
}
